//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

import { mapGetters, mapActions } from "vuex";
import template from "./paySetTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  data() {
    return {
      psd: "",
      psd2: "",
      showPsd: true,
      setSuccese: true,
    };
  },
  computed: {
    ...mapGetters(["accountData"]),
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.psd.focus();
    });
  },
  methods: {
    ...mapActions(["SET_ACCOUNT_DATA"]),
    setPsd() {
      let vm = this;
      let params = {
        token: localStorage.getItem("token"),
        psd: vm.psd,
        psd2: vm.psd2,
      };
      vm.$vux.loading.show();
      vm.$http
        .post(vm.urlRequest + "?m=api&c=user&a=setPayPSD", params)
        .then((res) => {
          console.log(res);
          // localStorage.setItem(moma,res);
          if (res.status == 0) {
            // vm.$vux.toast.text('设置支付密码成功！', 'bottom');
            let userAccountData = vm.accountData;
            userAccountData.is_set_paypassword = "1";
            vm.SET_ACCOUNT_DATA({
              Obj: userAccountData,
            });
            vm.setSuccese = false;
          } else {
            if (res.ret_msg && res.ret_msg != "") {
              vm.$vux.toast.show({
                text: res.ret_msg,
              });
            }
          }
          vm.$vux.loading.hide();
        })
        .catch((err) => {
          console.log(err);
          vm.$vux.loading.hide();
        });
    },
  },
  watch: {
    psd(newVal, oldVal) {
      if (
        isNaN(newVal) ||
        newVal.indexOf("e") != -1 ||
        newVal.indexOf(".") != -1
      ) {
        this.psd = oldVal;
        return;
      }
      let spans = document.querySelectorAll(".inp span");
      for (let i = 0; i < spans.length; i++) {
        spans[i].style.backgroundSize = "0";
      }
      for (let i = 0; i < this.psd.length; i++) {
        spans[i].style.backgroundSize = "auto";
      }
      if (this.psd.length == 6) {
        this.showPsd = false;
        this.$nextTick(() => {
          this.$refs.psd2.focus();
        });
      }
    },
    psd2(newVal, oldVal) {
      if (
        isNaN(newVal) ||
        newVal.indexOf("e") != -1 ||
        newVal.indexOf(".") != -1
      ) {
        this.psd2 = oldVal;
        return;
      }
      let spans = document.querySelectorAll(".inp2 span");
      for (let i = 0; i < spans.length; i++) {
        spans[i].style.backgroundSize = "0";
      }
      for (let i = 0; i < this.psd2.length; i++) {
        spans[i].style.backgroundSize = "auto";
      }
      if (this.psd2.length == 6) {
        if (this.psd2 != this.psd) {
          this.$vux.toast.text("两次密码输入不一致", "bottom");
          this.psd2 = "";
        } else {
          this.setPsd();
        }
      }
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/wallet/paysafe/paySet.vue
